package ma.xin.shi.configurations.ECU;

import java.util.ArrayList;
import java.util.List;

//import ma.xin.shi.configurations.SomeipAction.SomeipActionEntry;

public class ECUConfig {
    private List<ECUEntry> ecuList = new ArrayList<>();

    // 兼容旧调用：addECU（保留原方法名）
    public void addECU(ECUEntry entry) {
        addEcu(entry); // 调用统一的新方法
    }

    // 新增标准方法：addEcu（推荐使用这个）
    public void addEcu(ECUEntry entry) {
        ecuList.add(entry);
    }

    public boolean isECUExist(String ecu) {
    	boolean existFlg = false;
    	for (ECUEntry entry : ecuList) {
            if (ecu.equals(entry.getEcuName())) {
            	existFlg = true;
            	break;
            }
        }
        return existFlg;
    }

    public void creatECUIfNotExist(String ecu) {
    	if (false == isECUExist(ecu)) {
    		ECUEntry entry = new ECUEntry();
    		entry.setEcuName(ecu);
    		addEcu(entry);
    	}
    }

    // 删除指定索引的 ECU 条目
    public void removeEcu(int index) {
        if (index >= 0 && index < ecuList.size()) {
            ecuList.remove(index);
        }
    }

    // 获取指定索引的 ECU 条目
    public ECUEntry getEcuByIndex(int index) {
        if (index >= 0 && index < ecuList.size()) {
            return ecuList.get(index);
        }
        return null;
    }

    // 兼容旧调用：getAllECUs（保留原方法名）
    public List<ECUEntry> getAllECUs() {
        return getAllEcus(); // 调用新方法
    }

    // 新增标准方法：getAllEcus（推荐使用这个）
    public List<ECUEntry> getAllEcus() {
        return new ArrayList<>(ecuList); // 返回副本避免外部修改
    }

    // 更新指定索引的 ECU 数据
    public void updateEcu(int index, ECUEntry updatedEntry) {
        if (index >= 0 && index < ecuList.size()) {
            ecuList.set(index, updatedEntry);
        }
    }

    // 按名称获取 ECU
    public ECUEntry getECUByName(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry;
            }
        }
        return null;
    }

    // 按名称删除 ECU
    public boolean removeECU(String name) {
        return ecuList.removeIf(entry -> entry.getEcuName().equals(name));
    }

    // 按名称更新 ECU 数据
    public boolean updateECU(String name, ECUEntry updatedEntry) {
        for (int i = 0; i < ecuList.size(); i++) {
            if (ecuList.get(i).getEcuName().equals(name)) {
                ecuList.set(i, updatedEntry);
                return true;
            }
        }
        return false;
    }
    
    public String getECUmacAddress(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getMacAddress();
            }
        }
        return null;
    }
    
    public void setECUmacAddress(String name, String mac) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setMacAddress(mac);
                break;
            }
        }
    }
    
    public String getECUInitDelayMin(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getInitDelayMin();
            }
        }
        return null;
    }
    
    public void setECUInitDelayMin(String name, String initDelayMin) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setInitDelayMin(initDelayMin);
                break;
            }
        }
    }
    
    public String getECUInitDelayMax(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getInitDelayMax();
            }
        }
        return null;
    }
    
    public void setECUInitDelayMax(String name, String initDelayMax) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setInitDelayMax(initDelayMax);
                break;
            }
        }
    }

    public String getECURepetitionBaseDelay(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getRepetitionBaseDelay();
            }
        }
        return null;
    }

    public void setECURepetitionBaseDelay(String name, String repBaseDelay) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setRepetitionBaseDelay(repBaseDelay);
                break;
            }
        }
    }

    public String getECUrepetitionsMax(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getRepetitionsMax();
            }
        }
        return null;
    }
    
    public void setECUrepetitionsMax(String name, String max) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setRepetitionsMax(max);
                break;
            }
        }
    }
    
    public String getECUCyclicOfferDelay(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getCyclicOfferDelay();
            }
        }
        return null;
    }
    
    public void setECUCyclicOfferDelay(String name, String cyclicOfferDelay) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setCyclicOfferDelay(cyclicOfferDelay);
                break;
            }
        }
    }
    
    public String getECURequestResponseDelayMin(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getRequestResponseDelayMin();
            }
        }
        return null;
    }
    
    public void setECURequestResponseDelayMin(String name, String rspDelayMin) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setRequestResponseDelayMin(rspDelayMin);
                break;
            }
        }
    }

    public String getECURequestResponseDelayMax(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getRequestResponseDelayMax();
            }
        }
        return null;
    }
    
    public void setECURequestResponseDelayMax(String name, String rspDelayMax) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setRequestResponseDelayMax(rspDelayMax);
                break;
            }
        }
    }

    public String getECUremark(String name) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                return entry.getRemark();
            }
        }
        return null;
    }
    
    public void setECUremark(String name, String remark) {
        for (ECUEntry entry : ecuList) {
            if (entry.getEcuName().equals(name)) {
                entry.setRemark(remark);
                break;
            }
        }
    }
}
